import { Component, Vue, Watch } from 'vue-property-decorator';
import { Getter } from "vuex-class";
import { isEmpty, map, countBy, assign, compact, filter } from 'lodash';
import { findTree } from "@/utils";
@Component({
})
export class mixinAccess extends Vue {
    // 按钮权限配置
    permissionsData: any = {
    };
    // 字段权限配置
    fieldPermissionsData: any = {
    };
    // 获取用户权限数据
    @Getter("coreMenu/access") accessData: any;
    @Watch("accessData", { immediate: true })
    updateAccessData(v: any) {
        const me = this as any;
        if (!isEmpty(v)) {
            me.$nextTick(() => {
                // 获取权限名称
                const name = me.accessName || me.$route.name,
                    // 根据路由名称查找当前视图权限
                    data: any = findTree(v, { path: name });
                if (data) {
                    // 将权限数据转换为一维数组
                    let permissionsData: any = map(data.children, 'webPerms');
                    // 移除空数据
                    permissionsData = compact(permissionsData);
                    // 获取字段权限
                    const fieldPermissionsData = filter(permissionsData, function (item: any) {
                        return item.indexOf('@grid') >= 0
                    });
                    // 获取按钮权限
                    permissionsData = filter(permissionsData, function (item: any) {
                        return item.indexOf('@grid') < 0
                    });
                    // 将一维数组转换为对象
                    permissionsData = countBy(permissionsData, function (item: any) {
                        return item
                    })
                    // 与默认权限配置合并
                    // 这样写避免权限数据发生变化时，列表按钮组不能及时更新
                    me.permissionsData = assign({}, me.permissionsData, permissionsData);
                    // console.log('当前页面按钮权限', me.permissionsData);
                    if (!isEmpty(fieldPermissionsData)) {
                        // 设置按钮权限
                        me.fieldPermissionsData = fieldPermissionsData;
                        me.updateTable(fieldPermissionsData);
                         // console.log('当前页面字段权限', me.fieldPermissionsData);
                    }
                }
            });
        }
    }

    /**
     * 根据字段权限配置控制列表字段显示
     * 对应字段应新增以下配置
                visible: false,
                params: {
                    isAccess: true
                }
     *
     * @param {*} data 权限数据
     * @param {string} [gridName='xTable'] 表格ref
     * @memberof mixinAccess
     */
    updateTable(data: any, gridName: string = 'xTable', storeName: string = 'tableList') {
        const me = this as any, xTable = me.$refs[gridName];
        if (xTable) {
            // 遍历字段权限
            data.forEach((item: any) => {
                // 检查字段是否存在，如果存在就显示改字段
                const field = xTable.getColumnByField(item.split('@grid')[0]);
                if (field) {
                    field.visible = true;
                }
            })
            xTable.refreshColumn();
            me.loadStoreCount(gridName, storeName);
        }
    }
}
